package com.zut.hospital.controller.staff;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.staff.Office;
import com.zut.hospital.entity.staff.OfficeExample;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import com.zut.hospital.service.staff.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program:
 * @description: 无
 * @author: hux
 * @create: 2019-05-30 10:19
 **/
@Controller
@RequestMapping("/hospital")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OfficeMapper officeMapper;
    @Autowired
    HttpServletRequest request;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);


    public User setUser(String message) {
        User user = JSONObject.parseObject(message, User.class);
        return user;
    }


    @ResponseBody
    @RequestMapping(value = "/user")
    public String showUser(@RequestParam (value="page",required=false)Integer page,
                           @RequestParam(value="limit",required=false)Integer limit) {
        log.info("page"+page.toString());

            return userService.pageUser(page, limit);

        /*else {
            return userService.findByOfcId(page, limit, ofcId);
        }*/

    }

    @ResponseBody
    @RequestMapping(value = "/addUser",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String addUser(@RequestBody String data){
        int ofc_id=Integer.parseInt(JSONObject.parseObject(data).getString("ofc_id"));
        String ad_num = JSONObject.parseObject(data).getString("ad_num");
        String ad_password = JSONObject.parseObject(data).getString("ad_password");
        String ad_remark = JSONObject.parseObject(data).getString("ad_remark");
        int ad_power = Integer.parseInt(JSONObject.parseObject(data).getString("ad_power"));
        String ad_name = JSONObject.parseObject(data).getString("ad_name");

        OfficeExample officeExample=new OfficeExample();
        officeExample.createCriteria().andIdEqualTo(ofc_id);
        Office office=officeMapper.selectByExample(officeExample).get(0);

        int a=userService.addUser(office.getId(),ad_num,ad_password,ad_remark,ad_power,ad_name);
        if (a==0){
            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 0);
            layuiMap.put("msg", "failed");
            layuiMap.put("count", 0);
            layuiMap.put("data", null);
            return JSON.toJSONString(layuiMap);
        }

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }


    @ResponseBody
    @RequestMapping(value = "/deleteUser",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String deleteUser(@RequestBody String data){
        JSONObject jsonObject=JSONObject.parseObject(data);
        String key=jsonObject.getString("key");
        JSONArray jsonArray=JSONArray.parseArray(key);

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
            String ad_num = jsonObject1.getString("ad_num");
            log.info("ad_num:" + ad_num);
            int a = userService.deleteUser(ad_num);
            if (a == 0) {
                Map <String, Object> layuiMap = new HashMap <>();
                layuiMap.put("code", 0);
                layuiMap.put("msg", "failed");
                layuiMap.put("count", 0);
                layuiMap.put("data", null);
                return JSON.toJSONString(layuiMap);
            }
        }
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }
    @ResponseBody
    @RequestMapping(value = "/updateUser",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String updateUser(@RequestBody String data) {

        int id = JSONObject.parseObject(data).getInteger("id");
        String field = JSONObject.parseObject(data).getString("field");
        String value = JSONObject.parseObject(data).getString("value");

        userService.updateUser(id,field,value);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);
    }
    @ResponseBody
    @RequestMapping(value = "/duty",produces = "application/json;charset=utf-8",method = RequestMethod.GET)
    public String duty() {

        return userService.duty();
    }

    @ResponseBody
    @RequestMapping(value = "/updateduty",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String updateduty(@RequestBody String data) {

        String field = JSONObject.parseObject(data).getString("field");
        String value = JSONObject.parseObject(data).getString("value");
       userService.updateduty(field,value);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);
    }

    @ResponseBody
    @RequestMapping(value = "/selectByOfcId",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
     public String findByOfcId(int page,int limit,Integer ofcId){

        return  userService.findByOfcId(page,limit,ofcId);

      }

    @ResponseBody
    @RequestMapping(value = "/allUser",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public String findALlUser() {
        return userService.getAllUser();
    }



    }





